package com.yidong.laidian.laidian.controller;

import com.yidong.laidian.laidian.enums.ResultEnum;
import com.yidong.laidian.laidian.exception.ApiViewException;
import com.yidong.laidian.laidian.form.UserForm;
import com.yidong.laidian.laidian.util.ResultVoUtils;
import com.yidong.laidian.laidian.vo.Person;
import com.yidong.laidian.laidian.vo.ResultVO;
import com.yidong.laidian.laidian.vo.UserVO;
import jakarta.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @PostMapping("/user")
    public ResultVO<Object> createUserForm(@Valid @RequestBody UserForm userForm, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            System.out.println("错误信息：");
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
//            System.out.println("错误新列表：");
//            bindingResult.getAllErrors().forEach(error -> System.out.println(error.getDefaultMessage()));

            throw new ApiViewException(ResultEnum.ACCOUNT_NOT_EXIST.getCode(), bindingResult.getFieldError().getDefaultMessage());

        }

        return ResultVoUtils.success(userForm);
    }

    @GetMapping("/")
    public String index(){
        UserVO userVO = new UserVO();

        userVO.setName("laoliu");
        userVO.setAge(18);
        userVO.setSex("man");

        userVO.getSex();

        System.out.println(userVO.toString());

        Person person = new Person();
        person.setAge(20);

        System.out.println(person.getAge());

        System.out.println(person);

        return "index";
    }
}
